<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/6/6
 * Time: 15:51
 */

namespace App\Rep;


use App\Models\Appendance;
use App\Models\AppendanceLog;
use App\Models\Employee;

class AppendanceLogRep
{

    /**
     * 获取考勤列表
     * @param $params
     * 1:正常2：未打卡3：迟到4：休息5：请假6:早退
     */
    public function getAppendanceLogListRep($params){
        //员工列表
        $employee_user_list = Employee::Search($params)->select('id','user_id','employee_name')->paginate($params['per_page']??100);
        $start_time = $params['year_month']??date("Y-m")."-01";
        $end_time = date('Y-m-d', strtotime("$start_time +1 month -1 day"));
        $appendance = Appendance::where('id',1)->first();
        $period = \Carbon\CarbonPeriod::create($start_time, $end_time);
        $date_list = [];
        foreach ($period as $key => $date) {
            $date_list[] = $date->format('Y-m-d');
        }
        foreach($employee_user_list as $k=>$v){
            foreach($date_list as $kk=>$vv){
                $log[$kk]['date'] = date("d",strtotime($vv));
                $first = AppendanceLog::where('user_id',$v->user_id)
                        ->where('date',$vv)
                        ->where('appendance_time','<',$vv.' 12:00:00')
                        ->orderBy('appendance_time','asc')
                        ->first();
                $log[$kk]['first_time'] = $first->appendance_time??'';
                if($first){
                    if(strtotime($first->appendance_time) < strtotime($vv." ".$appendance->office_hours)){
                        $log[$kk]['first'] = 1;
                    }else{
                        $log[$kk]['first'] = 3;
                    }
                }else{
                    $log[$kk]['first'] = 2;
                }

                $last = AppendanceLog::where('user_id',$v->user_id)
                        ->where('date',$vv)
                        ->where('appendance_time','>',$vv.' 12:00:00')
                        ->orderBy('appendance_time','desc')
                        ->first();
                $log[$kk]['last_time'] = $last->appendance_time??'';
                if($last){
                    if($last->appendance_time >= $vv." ".$appendance->closing_time){
                        $log[$kk]['last'] = 1;
                    }else{
                        $log[$kk]['last'] = 6;
                    }
                }else{
                    $log[$kk]['last'] = 2;
                }

            }
            $employee_user_list[$k]['appendance_log'] = $log;
        }
        return $employee_user_list;
    }

    /**
     * 考勤打卡
     * @param $params
     */
    public function addAppendanceLogRep($params){
        $model = new AppendanceLog();
        $model->user_id = $params['user_id'];
        $model->date = date('Y-m-d');
        $model->appendance_time = date('Y-m-d H:i:s');
        $model->save();
        return $model;
    }

    /**
     * 补考勤
     * @param $params
     */
    public function supplementAppendanceLogRep($params){
        $model = new AppendanceLog();
        $model->user_id = $params['users_id'];
        //p($params['appendance_time']);
        //p(strtotime($params['appendance_time']));
        $model->date = date("Y-m-d",strtotime($params['appendance_time']));
        $model->appendance_time = $params['appendance_time'];
        $model->save();
        return $model;
    }
}